package org.example.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.user.domain.Bankcard;
import org.example.user.domain.Transfers;
import org.example.user.domain.User;
import org.example.user.mapper.TransfersMapper;
import org.example.user.service.BankcardService;
import org.example.user.service.TransfersService;
import org.example.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * @author Lenovo
 * @description 针对表【take_transfers】的数据库操作Service实现
 * @createDate 2024-06-26 13:01:46
 */
@Service
public class TransfersServiceImpl extends ServiceImpl<TransfersMapper, Transfers>
        implements TransfersService {
    @Autowired
    private UserService userService;
    @Autowired
    private BankcardService bankcardService;

    @Override
    @Transactional
    public boolean save1(Transfers transfers, User userDB, Bankcard bankcard) {
        System.out.println("我的银行卡：" + bankcard + "\n");
        userDB.setMoney(userDB.getMoney().add(transfers.getTransferAmount()));
        boolean b = userService.updateById(userDB);

        Transfers transfers1 = new Transfers();
        transfers1.setPayerId(userDB.getId());
        transfers1.setPayerBankid(transfers.getPayerBankid());
        transfers1.setPayeeId(userDB.getId());
        transfers1.setPayeeBankid(5);
        transfers1.setTransferAmount(transfers.getTransferAmount());
        transfers1.setTransferPurpose(transfers.getTransferPurpose());
        transfers1.setTransferPostscript("充值到钱包");
        transfers1.setCreateTime(new Date());
        int insert = baseMapper.insert(transfers1);
        System.out.println("已成功添加到转账记录中" + "\n");
        if (insert >= 0) {
            b = true;
        }
        System.out.println(b + "添加成功" + "\n");
        bankcard.setTransferLimit(bankcard.getTransferLimit().subtract(transfers1.getTransferAmount()));
        bankcard.setBalance(bankcard.getBalance().subtract(transfers1.getTransferAmount()));
        b = bankcardService.updateById(bankcard);

        System.out.println("修改银行卡余额现在为" + bankcard.getBalance() + "\n");
        return b;
    }

    @Override
    public boolean save2(Transfers transfers, User userDB, Bankcard one) {
//        对用户的钱包进行减
        userDB.setMoney(userDB.getMoney().subtract(transfers.getTransferAmount()));
        boolean b = false;
        b = userService.updateById(userDB);
        System.out.println("我的钱包余额：" + userDB.getMoney() + "\n" + "钱包提现到银行卡" + b);
        Transfers transfers1 = new Transfers();
        transfers1.setPayerId(userDB.getId());
        transfers1.setPayerBankid(5);
        transfers1.setPayeeId(userDB.getId());
        transfers1.setPayeeBankid(transfers.getPayerBankid());
        transfers1.setTransferAmount(transfers.getTransferAmount());
        transfers1.setTransferPurpose(transfers.getTransferPurpose());
        transfers1.setTransferPostscript("提现到银行卡");
        transfers1.setCreateTime(new Date());
        System.out.println("已经成功添加到转账记录中" + "\n");

        int insert = baseMapper.insert(transfers1);
        if (insert >= 0) {
            b = true;
        }
        one.setBalance(one.getBalance().add(transfers1.getTransferAmount()));

        b = bankcardService.updateById(one);
        return b;
    }
}




